package wxapp

import (
	"wxsdk/wxpay/core"
	. "wxsdk/wxpay"
	"wxsdk/wxpay/utils/mapstruct"
)

type RefundQueryRequest struct {
	Appid         string   `xml:"appid"`                    //微信分配的小程序ID
	MchId         string   `xml:"mch_id"`                   //微信支付分配的商户号
	SubAppid      string   `xml:"sub_appid,omitempty"`      //小程序的APPID,当前调起支付的小程序APPID(服务商特有参数)
    SubMchId      string   `xml:"sub_mch_id,omitempty"`     //微信支付分配的子商户号(服务商特有参数)
	DeviceInfo    string   `xml:"device_info,omitempty"`    //商户自定义的终端设备号，如门店编号、设备的ID等
	NonceStr      string   `xml:"nonce_str"`                //随机字符串
	Sign          string   `xml:"sign"`                     //签名
	SignType      string   `xml:"sign_type,omitempty"`      //签名类型
	TransactionId string   `xml:"transaction_id,omitempty"` //微信订单号(四选一)
	OutTradeNo    string   `xml:"out_trade_no,omitempty"`   //商户系统内部的订单号(四选一)
	OutRefundNo   string   `xml:"out_refund_no,omitempty"`  //商户退款单号(四选一)
	RefundId      string   `xml:"refund_id,omitempty"`      //微信退款单号(四选一)
}

type WXRefundCoupon struct {
	OutRefundNo         string   //商户退款单号
	RefundId            string   //微信退款单号
	RefundChannel       string   //退款渠道
	RefundFee           string   //申请退款金额
	SettlementRefundFee string   //退款金额
	CouponTypeM         string   //代金券类型
	CouponRefundFee     string   //总代金券退款金额
	CouponRefundCount   string   //退款代金券使用数量
	CouponRefundIdM     string   //退款代金券ID
	CouponRefundFeeM    string   //单个代金券退款金额
	RefundStatus        string   //退款状态
	RefundAccount       string   //退款资金来源
	RefundRecvAccout    string   //退款入账账户
	RefundSuccessTime   string   //退款成功时间
}

// 查询退款
type RefundQueryResponse struct {
	BaseResponse            `xml:",innerXml,omitempty"`
    TotalRefundCount   string `xml:"total_refund_count,omitempty"`   // 订单总退款次数
	TransactionId      string `xml:"transaction_id,omitempty"`       // 微信订单号
	OutTradeNo         string `xml:"out_trade_no,omitempty"`         // 商户订单号
	TotalFee           string `xml:"total_fee,omitempty"`            // 订单总金额
	SettlementTotalFee string `xml:"settlement_total_fee,omitempty"` // 应结订单金额
	FeeType            string `xml:"fee_type,omitempty"`             // 订单金额货币种类
	CashFee            string `xml:"cash_fee,omitempty"`             // 现金支付金额
	RefundCount        string `xml:"refund_count,omitempty"`         // 退款笔数
	/*
	OutRefundNoN         string `xml:"out_refund_no_$n,omitempty"`    //商户退款单号
	RefundIdN            string `xml:"refund_id_$n,omitempty"`        //微信退款单号
	RefundChannelN       string `xml:"refund_channel_$n,omitempty"`   //退款渠道
	RefundFeeN           string `xml:"refund_fee_$n,omitempty"`            //申请退款金额
	SettlementRefundFeeN string `xml:"settlement_refund_fee_$n,omitempty"` //退款金额
	CouponTypeNM         string `xml:"coupon_type_$n_$m,omitempty"`        //代金券类型
	CouponRefundFeeN     string `xml:"coupon_refund_fee_$n,omitempty"`     //总代金券退款金额
	CouponRefundCountN   string `xml:"coupon_refund_count_$n,omitempty"`   //退款代金券使用数量
	CouponRefundIdNM     string `xml:"coupon_refund_id_$n_$m,omitempty"`   //退款代金券ID
	CouponRefundFeeNM    string `xml:"coupon_refund_fee_$n_$m,omitempty"`  //单个代金券退款金额
	RefundStatusN        string `xml:"refund_status_$n,omitempty"`         //退款状态
	RefundAccountN       string `xml:"refund_account_$n,omitempty"`        //退款资金来源
	RefundRecvAccoutN    string `xml:"refund_recv_accout_$n,omitempty"`    //退款入账账户
	RefundSuccessTimeN   string `xml:"refund_success_time_$n,omitempty"`   //退款成功时间
	*/
	WXRefundCoupons      []WXRefundCoupon
}

func  RefundQuery(appId,mchId,subAppid,subMchId,appSecret,transactionId,orderIdStr,refundNo,refundId string) (RefundQueryResponse,error){
	client := core.GetWxPayClient(appId,mchId,appSecret,nil)
	var rsp RefundQueryResponse
	var req = new(RefundQueryRequest)
	if len(subAppid) > 0 {
	   req.SubAppid = subAppid
	}
	if len(subMchId) > 0 {
	   req.SubMchId = subMchId
	}
	req.TransactionId = transactionId
	req.OutTradeNo = orderIdStr         
	req.OutRefundNo = refundNo                    
	req.RefundId = refundId
	rspMap, err := client.ExcuteMap(RefundQueryUrl, req)
    if err != nil {
		return rsp, err
	}
	err = mapstruct.Map2Struct(rspMap, rsp, "xml")
	if err != nil {
		return rsp, err
	}
	return rsp, nil
}
